//
//  SGJTaskExamineDetailViewController.m
//  Sewage
//
//  Created by 优聚泰元 on 2017/6/7.
//  Copyright © 2017年 宗庆猛. All rights reserved.
//

#import "SGJTaskExamineDetailViewController.h"

#import "SGJTaskExamineDetailHeader.h"
#import "SGJTaskExamineCell.h"

#import "SGJNetworkTools.h"
#import "SGJTempObject.h"

#import <YYModel.h>

#import "SGJNetworkTools.h"

#import "SGJNormalReportViewController.h"
#import "SGJTroublesReportViewController.h"

@interface SGJTaskExamineDetailViewController ()<UITableViewDataSource,UITableViewDelegate,SGJTaskExamineCellDelegate>

@property (strong,nonatomic) UITableView *tableView;

@property (strong,nonatomic) NSArray *examineDetailList;

@property (strong,nonatomic) SGJTempObject *tempObject;

@end

@implementation SGJTaskExamineDetailViewController


-(void)mapItemClick{
    NSLog(@"绘线地图切换");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = self.grabOrderModel.id;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"地图切换"] style:UIBarButtonItemStylePlain target:self action:@selector(mapItemClick)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self loadData];
    [self setupTableView];
}

-(void)setupTableView{
    
    CGRect frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 120);
    UITableView *tableView = [[UITableView alloc] init];
    tableView.frame = CGRectMake(0, 0, frame.size.width, [UIScreen mainScreen].bounds.size.height);
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    SGJTaskExamineDetailHeader *header = [[NSBundle mainBundle] loadNibNamed:@"SGJTaskExamineDetailHeader" owner:nil options:nil].lastObject;
    header.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 130);
    tableView.tableHeaderView = header;
    tableView.tableFooterView = [[UIView alloc] init];
    //[tableView registerClass:[SGJGrabOrderCell class] forCellReuseIdentifier:@"grabOrder"];
    [tableView registerNib:[UINib nibWithNibName:@"SGJTaskExamineCell" bundle:nil] forCellReuseIdentifier:@"SGJTaskExamineCell"];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.tempObject.poiRecords.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SGJTaskExamineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SGJTaskExamineCell" forIndexPath:indexPath];
    
    cell.poiModel = self.tempObject.poiRecords[indexPath.row];
    //cell.grabOrderModel = self.grabOrderList[indexPath.row];
    // Configure the cell...
    
     cell.delegate = self;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

#pragma mark - SGJTaskExamineCellDelegate
-(void)normalButtonClickWithTaskExamineCell:(SGJTaskExamineCell *)taskExamineCell{
   
    
    SGJNormalReportViewController *normalVc = [[SGJNormalReportViewController alloc] init];
    normalVc.tempObject = self.tempObject;
    [self.navigationController pushViewController:normalVc animated:YES];
}

-(void)troubleButtonClickWithTaskExamineCell:(SGJTaskExamineCell *)taskExamineCell{
    SGJTroublesReportViewController *troubleReportVc = [[SGJTroublesReportViewController alloc] init];
    troubleReportVc.tempObject = self.tempObject;
    [self.navigationController pushViewController:troubleReportVc animated:YES];
}

-(void)mapButtonClickWithTaskExamineCell:(SGJTaskExamineCell *)taskExamineCell{
    NSLog(@"定点地图切换");
}


-(void)loadData{
    //examine {id：1111,type:0}
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"taskId"] = self.grabOrderModel.id;
    //param[@"type"] = self.grabOrderModel.type;//不再需要这个参数了。
    param[@"userId"]= [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
    
    [[SGJNetworkTools sharedNetworkTool] examineTaskWithUrlString:kExamineTaskDetailUrl parameters:param method:@"POST" callBack:^(id responseObject) {
        NSLog(@"巡检详情%@",responseObject);
        
        NSData *jsonData = [responseObject[@"retData"] dataUsingEncoding:NSUTF8StringEncoding];
        //
        NSDictionary *tempDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:NULL];
        //
        //        self.poiRecordsList = [NSArray yy_modelArrayWithClass:[SGJPoiModel class] json:tempDict[@"poiRecords"]];
        
        SGJTempObject *tempObject = [SGJTempObject yy_modelWithDictionary:tempDict];
        self.tempObject = tempObject;
        
        [self.tableView reloadData];

    }];
}

@end
